package org.rslai.tcedit.jms;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * JMS 消息监听器，处理 objectmesage。
 * 
 * @author rslai
 * 
 * @spring.bean id="jmsListener"
 */
public class JMSListener implements MessageListener {
	private Log logger = LogFactory.getLog(getClass());
	private String jmsLogMsg;

	public void onMessage(Message message) {
		if (message instanceof ObjectMessage) {
			try {
				Object object = ((ObjectMessage) message).getObject();
				if (object instanceof JmsService) {
					JmsService jmsService = (JmsService) object;
					jmsLogMsg = jmsService.toString();
					logger.info("JMS call " + jmsLogMsg);
					jmsService.run();
				}
			} catch (JMSException e) {
				logger.warn("JMS call failed : " + jmsLogMsg , e);
			}
		}
	}

}
